﻿@using PMS.Web.Models.Customers
@{
    Layout = "_AdminHomeLayout";
}
@model CustomerModel

<fieldset class="layui-elem-field layui-field-title" style="margin-top: 10px;">
    <legend>当前用户：@Model.LoginName (@Model.NickName)</legend>
</fieldset>
<div class="layui-container">
    <div class="layui-form-item">
        <div id="customerRoleTransfer"></div>
        <select id="AvailableCustomerRoles" asp-items="@Model.AvailableCustomerRoles" class="layui-hide">
        </select>
        <input type="text" id="SelectedCustomerRoleIds" value="@(string.Join(",", Model.SelectedCustomerRoleIds))" class="layui-hide" />
        <input type="text" id="CustomerId" value="@Model.Id" class="layui-hide" />
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="button" id="assignRoleSubmitBtn" class="layui-btn">
                保存提交
            </button>
        </div>
    </div>
</div>

@section Scripts{
    <script type="text/javascript">
        layui.use(['jquery', 'lodash', 'http', 'button', 'popup', 'transfer'], function () {
            var $ = layui.jquery,
                lodash = layui.lodash,
                http = layui.http,
                button = layui.button,
                popup = layui.popup,
                transfer = layui.transfer;

            var initPageComponent = function () {
                var transferDatas = [];
                $('#AvailableCustomerRoles > option').each(function () {
                    var data = {
                        "value": $(this).val(),
                        "title": $(this).text(),
                    };
                    transferDatas.push(data);
                });
                var assginedCustomerRoleIds = lodash.split($("#SelectedCustomerRoleIds").val(), ',');
                transfer.render({
                    elem: '#customerRoleTransfer'
                    , id: 'customerRoleTransfer'
                    , title: ['所有角色', '已分配角色'] 
                    , data: transferDatas
                    , height: 320
                    , value: assginedCustomerRoleIds
                });
            };

            initPageComponent();
            //保存提交
            $("#assignRoleSubmitBtn").on("click", function () {
                var transferSelectedData = transfer.getData('customerRoleTransfer');
                if (transferSelectedData.length <= 0) {
                    popup.failure("请选择至少一个用户角色");
                    return false;
                }
                var selectedRoleIds = lodash.map(transferSelectedData, 'value');
                var loadbtn = null;
                http.post(backendwebapiConfig.customerroleassign, {
                    Id: $("#CustomerId").val(),
                    SelectedCustomerRoleIds: selectedRoleIds
                }, function () { //beforeSend
                        loadbtn = button.load({
                            elem: '#assignRoleSubmitBtn',
                            time: 10000
                        });
                    }
                ).done(function (response) {
                    if (response.state && response.state === 200) {
                        popup.success(response.message, function () {
                            PMSUtils.closePopupWindow();
                        });
                    }
                }).always(function () {
                    loadbtn.stop(function () { });
                });
            });
        });
    </script>
}
